package com.zengge.zbc.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.zengge.zbc.exception.ErrorCode;
import com.zengge.zbc.exception.ServiceException;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Result<T>{


    private int code;
    private T payload;
    private String message;

    public Result() {

    }

    public Result(T payload) {
        this.code = ResultCode.SUCCEED.code;
        this.payload = payload;
    }

    public Result(ErrorCode errorCode) {
        this.code = errorCode.code;
        this.message = errorCode.message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getPayload() {
        return payload;
    }

    public void setPayload(T payload) {
        this.payload = payload;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static <T>Result<T> create(T payload){
        return new Result<>(payload);
    }

    public static Result<String> create(ServiceException exception){
        return new Result<>(exception.getErrorCode());
    }
}
